package com.zenika.zenbinding.client.handler;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Widget;
import com.zenika.zenbinding.client.model.ValueModel;

public class CheckBoxHandler extends ValueBindingHandler implements ClickHandler {
  
  private CheckBox checkBox;
  private ValueModel model;
  
  public CheckBoxHandler(CheckBox checkBox, ValueModel model) {
    super();
    this.checkBox = checkBox;
    this.model = model;
    this.model.setHandler(this);
    this.bind();
  }
  
  public Object getValue() {
    return new Boolean(this.checkBox.getValue());
  }
  
  public Widget getWidget() {
    return this.checkBox;
  }
  
  public void onClick(ClickEvent event) {
    String result = (this.checkBox.getValue()) ? "true" : "false";
    this.fireChange(result, this);
    
  }
  
  public void setErrorStyle() {
  // TODO Auto-generated method stub
  }
  
  public void setRegularStyle() {
  // TODO Auto-generated method stub
  }
  
  private void bind() {
    if ( this.model.getValue().equals("true") ) {
      this.checkBox.setValue(true);
    } else {
      this.checkBox.setValue(false);
    }
    this.checkBox.addClickHandler(this);
  }
  
}
